package com.jn.erp.security.sysservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jn.erp.common.constant.UserConstants;
import com.jn.erp.common.utils.StringUtils;
import com.jn.erp.security.entity.SysUser;
import com.jn.erp.security.mapper.SysUserMapper;
import com.jn.erp.security.sysservice.SysUserService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <pre>
 *
 * </pre>
 *
 * @author sunjiyong
 * @since 2024/8/28 15:45
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    /**
     * 根据条件分页查询已分配用户角色列表
     *
     * @param user 用户信息
     * @return 用户信息集合信息
     */
    @Override
//    @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectAllocatedList(SysUser user) {
        return baseMapper.selectAllocatedList(user);
    }

    @Override
    public List<SysUser> selectUnallocatedList(SysUser user) {
        return baseMapper.selectUnallocatedList(user);
    }

    @Override
    public List<SysUser> selectUserList(SysUser user) {
        return baseMapper.selectUserList(user);
    }

    @Override
    public SysUser checkUserInDept(SysUser user) {
        return baseMapper.checkUserInDept(user);
    }


    //校验是否为其他机构下已有用户
    @Override
    public boolean checkUserNameDept(SysUser user){
        int i = baseMapper.checkUserNameDept(user);
        return i>0;
    }

    /**
     * 校验用户名称是否唯一
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean checkUserNameUnique(SysUser user)
    {
        Long userId = StringUtils.isNull(user.getId()) ? -1L : user.getId();
        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        qw.eq("name",user.getName());
        SysUser info = baseMapper.selectOne(qw);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkPhoneUnique(SysUser user)
    {
        Long userId = StringUtils.isNull(user.getId()) ? -1L : user.getId();
        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        qw.eq("mobile",user.getMobile());
        SysUser info = baseMapper.selectOne(qw);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkEmailUnique(SysUser user)
    {
        Long userId = StringUtils.isNull(user.getId()) ? -1L : user.getId();
        QueryWrapper<SysUser> qw=new QueryWrapper<>();
        qw.eq("email",user.getEmail());
        SysUser info = baseMapper.selectOne(qw);
        if (StringUtils.isNotNull(info) && info.getId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public boolean updateUserAvatar(Long userId, String avatar) {
        SysUser eu=new SysUser();
        eu.setId(userId);
        eu.setAvatar(avatar);
        return baseMapper.updateById(eu)>0;
    }
}
